<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var Weight = function (price, pathLength) {
        this.price = price;
        this.pathLength = pathLength;
      };

      var Road = function (path, len, price) {
        this.path = path.slice(0);
        this.len = len;
        this.price = price;
      };

      var PathsSourceTarget = (graph, start, target) => {
        let paths = [];
        let road = new Road([], 0, 0);
        let visited = new Array(graph.length).fill(0);

        let travelGraph = (vertex, target, road, visited, price, len) => {
          if (vertex == target) {
            visited[vertex] = 1;

            road.path.push(vertex);
            road.len += len;
            road.price += price;
            paths.push(road);

            return;
          }

          visited[vertex] = 1;
          road.path.push(vertex);
          road.len += len;
          road.price += price;

          for (let i = 0; i < graph.length; i++) {
            if (visited[i] == 0 && graph[vertex][i].pathLength != -1) {
                let visTemp = visited.slice(0);
                let roadTemp = new Road(road.path,road.len,road.price);
                travelGraph(
                i,
                target,
                roadTemp,
                visTemp,
                graph[vertex][i].price,
                graph[vertex][i].pathLength
              );
            }
          }

          return;
        };

        travelGraph(start, target, road, visited, 0, 0);
        return paths;
      };
    
      
      // let graph = [];
      // for (let i = 0; i < 5; i++) {
      //   let item = [];
      //   for (let j = 0; j < 5; j++) {
      //     let weight = new Weight(0, -1);
      //     if (i == j) {
      //       weight.pathLength = 0;
      //     }
      //     item.push(weight);
      //   }

      //   graph.push(item);
      // }
      // console.log("graph = ", graph);

      // let weight1 = new Weight(2, 2);
      // let start = 0;
      // let target = 1;
      // graph[start][target] = weight1;
      // graph[target][start] = weight1;

      // let weight2 = new Weight(4, 4);
      // start = 0;
      // target = 4;
      // graph[start][target] = weight2;
      // graph[target][start] = weight2;

      // let weight3 = new Weight(3, 3);
      // start = 0;
      // target = 3;
      // graph[start][target] = weight3;
      // graph[target][start] = weight3;

      // let weight4 = new Weight(5, 5);
      // start = 1;
      // target = 4;
      // graph[start][target] = weight4;
      // graph[target][start] = weight4;

      // let weight5 = new Weight(7, 7);
      // start = 1;
      // target = 2;
      // graph[start][target] = weight5;
      // graph[target][start] = weight5;

      // let weight6 = new Weight(9, 9);
      // start = 2;
      // target = 3;
      // graph[start][target] = weight6;
      // graph[target][start] = weight6;

      // console.log("graph = ", graph);

      let graph = [
  [
  { price: 0, pathLength: 0 },
  { price: 700, pathLength: 1180 },
  { price: 180, pathLength: 497 },
  { price: 282, pathLength: 1378 },
  { price: 930, pathLength: 1711 },
  { price: 80, pathLength: 340 }
  ],
  [
  { price: 700, pathLength: 1180 },
  { price: 0, pathLength: 0 },
  { price: 577, pathLength: 910 },
  { price: 0, pathLength: -1 },
  { price: 626, pathLength: 1062 },
  { price: 0, pathLength: -1 }
  ],
  [
  { price: 180, pathLength: 497 },
  { price: 577, pathLength: 910 },
  { price: 0, pathLength: 0 },
  { price: 293, pathLength: 2785 },
  { price: 681, pathLength: 1383 },
  { price: 245, pathLength: 642 }
  ],
  [
  { price: 282, pathLength: 1378 },
  { price: 0, pathLength: -1 },
  { price: 293, pathLength: 2785 },
  { price: 0, pathLength: 0 },
  { price: 402, pathLength: 4131 },
  { price: 0, pathLength: -1 }
  ],
  [
  { price: 930, pathLength: 1711 },
  { price: 626, pathLength: 1062 },
  { price: 681, pathLength: 1383 },
  { price: 402, pathLength: 4131 },
  { price: 0, pathLength: 0 },
  { price: 930, pathLength: 1893 }
  ],
  [
  { price: 80, pathLength: 340 },
  { price: 0, pathLength: -1 },
  { price: 245, pathLength: 642 },
  { price: 0, pathLength: -1 },
  { price: 930, pathLength: 1893 },
  { price: 0, pathLength: 0 }
  ]
]

console.log("res",PathsSourceTarget(graph,2,4));
    </script>
  </body>
</html>



